<?php
set_include_path('/alidata/www/xinjieco/auto/');

require_once "class/mysql_connect.php";
require_once "class/function_curl.php";
require_once "class/function_cutwords.php";


set_time_limit(0);
$time = time();
$now_day = date(Ymd);

$num_listres = mysql_fetch_array(mysql_query("select count(*) as num from cdb_xinjieco_auto_listres"));

$taskid = mysql_fetch_array(mysql_query("select taskid from cdb_xinjieco_auto_task WHERE now_day = '$now_day' AND type = 'list' ORDER BY id DESC LIMIT 1 "));


if ($taskid && ($taskid[taskid] < $num_listres[num])) {
	$taskid = $taskid[taskid] + 1;
}else{
	$taskid = 1;
}



$listres = mysql_fetch_array(mysql_query("SELECT t1.*, t2.* FROM cdb_xinjieco_auto_listres t1, cdb_xinjieco_auto_listconf t2 WHERE t1.id = '$taskid' AND t1.type = t2.type"));


//抓取数据
$res = get_url($listres[url]);

if ($listres[is_gbk] == 1) {
	$res = iconv("gb2312", "UTF-8", $res);
}


if ($listres[page_res1_l]) {
	$page_res1_l = htmlspecialchars_decode($listres[page_res1_l]);
	$page_res1_r = htmlspecialchars_decode($listres[page_res1_r]);
	$res = str_substr("$page_res1_l", "$page_res1_r", $res);
}

/*
echo htmlspecialchars("\"");
exit();
*/

$page_res = htmlspecialchars_decode($listres[page_res]);
//输出列表页链接
preg_match_all("$page_res", $res, $page);


foreach ($page[1] as $k => $outpage) {
	


	$outpage_res_l = htmlspecialchars_decode($listres[outpage_res_l]);
	$outpage_res_r = htmlspecialchars_decode($listres[outpage_res_r]);
	$outpage = str_substr("$outpage_res_l", "$outpage_res_r", $outpage);
	//echo $outpage."\n";



    $query = mysql_query("select * from cdb_xinjieco_auto_list WHERE res = '$outpage'");//重复性
    if (mysql_num_rows($query)) {
    	$msg = ' —— 重复列表跳过。。。<br/>';
    }else{
		$sql = "INSERT INTO cdb_xinjieco_auto_list (fid, title, res, status, created_time) VALUES ('$listres[fid]', '', '$outpage', '0', '$time')";
		if (mysql_query($sql)) {
			$msg = "列表输入成功<br/>";
		}else{
			$msg = "连接出错".mysql_error();
		}
	}
	
	echo $msg;
	if ($k > 5) {
		break;
	}
}


mysql_query("INSERT INTO cdb_xinjieco_auto_task (taskid, now_day, type) VALUES ('$taskid', '$now_day', 'list')");



echo "<font color='red'>列表全部完成！</font><br/>";


